/*
*
*  收藏内容数据模型
*  负责收藏内容部分数据
*
*/
const db = require('../../config/db')
module.exports = {
    // 编写添加收藏内容方法
    // 参数：collect_content_id，collect_content_create_time,collect_content_title,user_id,content_id
    add(collect_content_id,collect_content_create_time,collect_content_title,user_id,content_id) {
        // console.log({collect_content_id,collect_content_create_time,collect_content_title,user_id,content_id})
        return new Promise((resolve, reject) => {
            db.query("select * from db_collect_content where content_id='" + content_id + "' and user_id = '"+user_id+"'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '查询收藏内容名称失败。' })
                } else {
                    // console.log('rows',rows)
                    if (rows.length == 0) {
                        db.query("insert into db_collect_content(collect_content_id,collect_content_create_time,collect_content_title,user_id,content_id) values('" + collect_content_id + "','" + collect_content_create_time + "','" + collect_content_title + "','"+user_id+"','"+content_id+"')", (err, rows) => {
                            if (err) {
                                resolve({ code: -1, msg: '添加收藏内容失败。' ,err })
                            } else {
                                resolve({ code: 200, msg: '添加收藏内容成功。' })
                            }
                        })
                    } else {
                        resolve({ code: 201, msg: '已添加收藏内容。' })
                    }
                }
            })
        })
    },
    
    // 编写删除收藏内容方法
    // 参数：user_id,content_id
    remove(user_id,content_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_collect_content where user_id='" + user_id + "' and content_id = '"+content_id+"'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '查询收藏内容名称失败。' })
                } else {
                    db.query("delete from  db_collect_content where user_id='" + user_id + "' and content_id = '"+content_id+"'", function (err, rows) {
                        if (err) {
                            resolve({ code: -1, msg: '删除收藏内容失败。' })
                        } else {
                            resolve({ code: 200, msg: '移除收藏内容成功。' })
                        }
                    })
                }
            })
        })
    },

    // 编写查询收藏内容方法
    // 参数：无
    list() {
        return new Promise((resolve, reject) => {
            db.query("select * from db_collect_content", (err, rows) => {
                if (err) {
                    // 反馈信息给客户端
                    resolve({ code: -1, msg: '查询收藏内容失败。' })
                } else {
                    resolve({ code: 200, msg: '查询收藏内容成功。', result: rows })
                }
            })
        })
    },

    my_list(user_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_collect_content where user_id = '"+user_id+"'", (err, rows) => {
                if (err) {
                    // 反馈信息给客户端
                    resolve({ code: -1, msg: '查询收藏内容失败。' })
                } else {
                    resolve({ code: 200, msg: '查询收藏内容成功。', result: rows })
                }
            })
        })
    },

    // 编写获取广告详情的方法
    // 参数：user_id,content_id
    detail(user_id,content_id) {
        return new Promise((resolve, reject) => {
            // 查询语句
            let sql = 'select * from db_collect_content where user_id = "' + user_id + '" and content_id = "'+content_id+'"'
            // 调用查询方法
            db.query(sql, function (err, rows) {
                if (err) {
                    resolve({
                        code: -1,
                        msg: '查询收藏内容失败。'
                    })
                } else {
                    if(rows.length == 0) {
                        resolve({
                            code: -1,
                            result: [],
                            msg: '查询收藏内容成功。'
                        });
                    }else {
                        resolve({
                            code: 200,
                            result: rows,
                            content_id: content_id,
                            msg: '查询收藏内容成功。'
                        });
                    }
                    
                }
            });
        })
    },
    // 统计收藏
    // 参数：user_id
    total(user_id) {
        return new Promise((resolve, reject) => {
            db.query("select * from db_collect_content where user_id='" + user_id + "'", (err, rows) => {
                if (err) {
                    resolve({ code: -1, msg: '收藏查询次数失败。' })
                } else {
                    if (rows.length == 0) {
                        resolve({ code: 200, msg: '查询收藏次数。', total: 0 })
                    } else {
                        let count = 0;
                        for (let i = 0; i < rows.length; i++) { count++; }
                        resolve({ code: 200, msg: '查询收藏次数。', total: count })
                    }
                }
            })
        })
    },
}